package com.fourforfo.fourmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fourforfo.fourmall.product.dao.BrandDao;
import com.fourforfo.fourmall.product.dao.CategoryDao;
import com.fourforfo.fourmall.product.entity.BrandEntity;
import com.fourforfo.fourmall.product.entity.CategoryEntity;
import com.fourforfo.fourmall.product.service.BrandService;
import com.fourforfo.fourmall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.Query;

import com.fourforfo.fourmall.product.dao.CategoryBrandRelationDao;
import com.fourforfo.fourmall.product.entity.CategoryBrandRelationEntity;
import com.fourforfo.fourmall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandService brandService;

    @Autowired
    CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        //1. 根据brandId和catelogId查询其name属性
        BrandEntity brandEntity = brandService.getById(brandId);
        CategoryEntity categoryEntity = categoryService.getById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String brandName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(brandName);
        this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Override
    public void updateCategory(Long catelogId, String catelogName) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogName(catelogName);
        this.update(categoryBrandRelationEntity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catelogId));
    }

    /**
     * 获取当前分类的所有品牌
     * @param catId 分类id
     * @return 品牌实体类集合
     */
    @Override
    public List<BrandEntity> listBrandByCatId(Long catId) {

        //获取所有brandId
        List<CategoryBrandRelationEntity> relationEntities = this.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
        List<Long> brandIds = relationEntities.stream().map((item)->{
            return item.getBrandId();
        }).collect(Collectors.toList());
        if(brandIds!=null&&brandIds.size()>0){
            return brandService.listByIds(brandIds);
        }

        return new ArrayList<>();
    }

}